package com.voja.salonschedule.customers;

import java.util.ArrayList;
import java.util.Arrays;

import com.voja.salonschedule.R;
import com.voja.salonschedule.data.Customer.CustomerItem;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PhotoAdapter extends ArrayAdapter<String>{

    private int          resourceId;
    private CustomerItem customer;
    private ArrayList<String> options;
    private ArrayList<Integer> optionIds;
    
    public DialogInterface.OnClickListener listener;
    
    public PhotoAdapter(Context context, int resourceId, CustomerItem customer, String[] options) {
        super(context, resourceId);
        
        this.resourceId = resourceId;
        this.customer   = customer;
        this.options    = new ArrayList<String>(Arrays.asList(options));
        this.optionIds  = new ArrayList<Integer>();
        for (int i=0; i<this.options.size(); i++) {
            this.optionIds.add(i);
        }
        
        if (customer.bitmap() == null) {
            for (int i=0; i<4; i++) {
                this.options.remove(0);
                this.optionIds.remove(0);
            }
        }
        
        setOptionListener();
    }
    
    @Override
    public int getCount() {
        return options.size();
    }
    
    @Override
    public String getItem(int index) {
        return options.get(index);
    }
    
    @Override
    public View getView(int index, View view, ViewGroup parent) {
        if (view == null) {
            view = ((Activity)getContext()).getLayoutInflater().inflate(resourceId, parent, false);
        }
        view.setId(index);
        
        // set label
        TextView optionName = (TextView)view.findViewById(R.id.txtOptionName);
        optionName.setText(getItem(index));
       
        // set icon
        ImageView imgIcon = (ImageView)view.findViewById(R.id.imgOptionIcon);
        imgIcon.setImageResource(getResourceFor(options.get(index)));

        
        return view;
    }
    
    public void setOptionListener() {
        listener = new PhotoOptionClickListener(getContext(), customer, options, optionIds);
    }
    
    public void handleOption(int index) {
        android.util.Log.v("option", "selected: " + index);
    }
    
    public int getResourceFor(String option) {
        int optionId = this.optionIds.get(this.options.indexOf(option));
        int id = R.drawable.blank;
        switch(optionId) {
        case 0:
            id = R.drawable.search;
            break;
        case 1:
            id = R.drawable.rotate_left;
            break;
        case 2:
            id = R.drawable.rotate_right;
            break;
        case 3:
            id = R.drawable.remove;
            break;
        case 4:
            id = R.drawable.gallery;
            break;
        case 5:
            id = R.drawable.camera;
            break;
        }
        
        return id;
    }

}
